﻿using System;
using System.Windows.Forms;
using System.Threading;
using SantaSLittleIrcHelper.Irc;


namespace SantaSLittleIrcHelper
{

    class Program
    {

        public static Program instance { get; private set; }
        public GUI GUI { get; private set; }
        public IrcConnection IRC { get; private set; }
        public String nickname { get; private set; }
        public Process currentProcess { get; private set; }
        public ConnectionStatus connectionStatus { get; private set; }
        public CurrentClientConfig ccc { get; private set; }
        public Settings settings;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Languages.Instance.load();
            instance = new Program();
            instance.run();
        }

        public void run()
        {
            this.settings = new Settings();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            this.GUI = new GUI();
            this.IRC = new IrcConnection();
            this.connectionStatus = new ConnectionStatus();

            this.ccc = new CurrentClientConfig();
            this.ccc.init();

            Application.Run(this.GUI);
        }

        public void connect()
        {
            IRC.OnReadLine += new ReadLineEventHandler(OnReadLine);
            IRC.OnWriteLine += new WriteLineEventHandler(OnWriteLine);
            IRC.OnDisconnected += new EventHandler(OnDisconnected);
            this.nickname = uniqNick();
            IRC.Connect(Program.instance.settings.ircServer, Program.instance.settings.ircPort);
            IRC.WriteLine(Rfc2812.Nick(this.nickname), Priority.Critical);
            IRC.WriteLine(Rfc2812.User(this.nickname, 0, "SLIH"), Priority.Critical);
            Thread IRCThread = new Thread(new ThreadStart(IRC.Listen));
            IRCThread.Start();
        }

        public void disconnect()
        {
            Program.instance.IRC.WriteLine(Rfc2812.Quit("Bye bye darling"));
        }

        private static string uniqNick()
        {
            string ret = string.Empty;
            System.Text.StringBuilder SB = new System.Text.StringBuilder();
            string Content = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
            Random rnd = new Random();
            for (int i = 0; i < 8; i++) SB.Append(Content[rnd.Next(Content.Length)]);
            return SB.ToString();
        }

        public void consoleWrite(String s)
        {
            this.consoleWrite(s, Consoles.feedback);
        }

        public void consoleWrite(String s, Consoles c)
        {
            if (s.Contains("[br]"))
            {
                String[] strings = s.Split(new string[] { "[br]" }, StringSplitOptions.None);
                foreach (String a in strings) this.consoleWrite(a, c);
                return;
            }

            this.GUI.addConsoleMessage(s, c);
        }

        internal void startProcess(Process Process)
        {
            if (this.currentProcess != null)
            {
                this.consoleWrite("Please wait until current process is finished");
            }
            else
            {
                this.currentProcess = Process;
                this.currentProcess.start();
            }
        }

        internal void killProcess()
        {
            this.currentProcess = null;
        }

        public void terminate()
        {
            this.settings.save();
            Languages.Instance.save();
            this.ccc.save();
        }

        void OnWriteLine(object sender, WriteLineEventArgs e)
        {
            this.consoleWrite(e.Line, Consoles.sent);
        }

        void OnDisconnected(object sender, EventArgs e)
        {
            IRC.OnReadLine -= new ReadLineEventHandler(OnReadLine);
            IRC.OnWriteLine -= new WriteLineEventHandler(OnWriteLine);
        }


void OnReadLine(object sender, ReadLineEventArgs e)
{
    String msg = e.Line;
    consoleWrite(msg, Consoles.received);
    if (e.Line.StartsWith("PING"))
    {
        string mServer = e.Line.Split(' ')[1];
        this.IRC.WriteLine("PONG " + mServer, Priority.Critical);
    }
    if (currentProcess != null && msg.Contains(currentProcess.triggerString)) currentProcess.step(msg);
}

    }
}
